
# Dictionary

# List
# List 是一种靠数字作为索引，来访问其中的内容的容器。
# nums = [11, 12, 13, 14, 15]
# 用什么东西来访问 nums 里的内容呢？ - 用数字作为索引
# nums[0]
# nums[1]

# 在现实世界里，有很多时候，我们希望用于获取数据的索引，都不是数字
# 字典
# EEDic["red"] - Red meaning
# Info["Daniel"] - ????
# Cars["粤??????"] - xxxxx

# 当我们希望使用非数字类型作为索引获取内容的时候，我们可以定义字典
# 字典维护的是一个值和另一个值的对应关系
# 一、定义
eedic = {
    # 索引 : 值
    # key : value
    "red" : "The color of blood."
}

print(eedic["red"])

car = {
    "brand" : "Ford",
    "model" : "Mustang",
    "year"  : 1964
}

print(car["brand"])
print(car["model"])
print(car["year"])

# 二、Dictionary 的常用方法
# 2.1 获取字典的记录条数 - len
# 一对 key:value 叫做字典的一条记录
print(len(eedic)) # 1
print(len(car))   # 3

# 2.2 Dictionary 的类型 - type
print(type(eedic)) # <class 'dict'>

bmw = dict(brand = "bmw", model = "series 3", year = 2022)
print(bmw["brand"])

# 2.3 练习
# 尝试用字典来描述一下你身边的一个事物
my_book = {
    "title": "Python programming",
    "pages": 200,
    "level": "Beginner"
}

print(my_book["title"])
print(my_book["pages"])
print(len(my_book)) # 3

# my_mobile - Dictionary
my_mobile = {
    # key    : value
    "model"  : "Apple iPhone", # record
    "storage": 128,
    "size"   : 6.7,
    "weight" : 500
}

print(len(my_mobile)) # 4

little_dic = {
    "red" : "The color of blood",
    "green" : "The color of grass or spring",
    "yellow": "The color of sun or the butter or the center of an egg."
}
print(len(little_dic)) #3
print(little_dic["green"]) # "The color of grass or spring"

# 对于这个数字列表 [11, 12, 13, 14, 15]
# 为它创建一个字典，这个字典的 key 是每一个数字在列表中的位置，value 是对应位置上的值
numberDic = {
    "0" : 11,
    "1" : 12,
    "2" : 13,
    "3" : 14,
    "4" : 15
}

print(len(numberDic)) # 5
print(numberDic["3"]) # 14

# Useful methods of dictionries
# 1. get() - 
print(little_dic.get("red"))
print(little_dic.get("brown"))
# print(little_dic["brown"]) - This will cause an error.

# 2. clear() - 
# little_dic.clear() - This will clear the whole dictionary.
# print(little_dic)

# 3. copy
# Dictionary in Python is a reference type.
dicA = {
    "I": 1,
    "II": 2
}

# dicB and dicA refer to the same dictionary.
dicB = dicA

# The deep copy of dicA
dicB = dicA.copy()

dicB["I"] = "one"
print(dicA["I"]) # 1
print(dicB["I"]) # one

# Get all the keys of a dictionary
print(little_dic.keys())

# Get all the values of a dictionary
print(little_dic.values())

# Remove a record for a dictionary
little_dic.pop("yellow")
print(little_dic)

# Remove a non exist key will trigger an error.
# little_dic.pop("11")

del little_dic["red"]
print(little_dic)

# --------
# A collection type
# 增删改查
# C - create
# U - update
# R - read
# D - delete
# --------
# keys() / values() / del / copy()